package com.education.platform.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaCheckRole;
import cn.dev33.satoken.stp.StpUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.education.platform.common.Result;
import com.education.platform.dto.notification.NotificationCreateDTO;
import com.education.platform.entity.Notification;
import com.education.platform.service.NotificationService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 通知控制器
 *
 * @author education-platform
 * @since 2025-10-27
 */
@Tag(name = "系统通知管理", description = "系统通知管理相关接口")
@RestController
@RequestMapping("/api/admin/notifications")
@RequiredArgsConstructor
@SaCheckLogin
public class NotificationController {

    private final NotificationService notificationService;

    @Operation(summary = "创建通知", description = "创建新通知")
    @PostMapping
    public Result<Long> createNotification(@Validated @RequestBody NotificationCreateDTO dto) {
        Long notificationId = notificationService.createNotification(dto);
        return Result.success(notificationId);
    }

    @Operation(summary = "更新通知", description = "更新通知信息")
    @PutMapping("/{id}")
    public Result<Void> updateNotification(@PathVariable Long id,
                                           @Validated @RequestBody NotificationCreateDTO dto) {
        notificationService.updateNotification(id, dto);
        return Result.success();
    }

    @Operation(summary = "删除通知", description = "删除通知")
    @DeleteMapping("/{id}")
    public Result<Void> deleteNotification(@PathVariable Long id) {
        notificationService.deleteNotification(id);
        return Result.success();
    }

    @Operation(summary = "获取通知详情", description = "根据ID获取通知详细信息")
    @GetMapping("/{id}")
    public Result<Notification> getNotificationDetail(@PathVariable Long id) {
        Notification notification = notificationService.getNotificationById(id);
        return Result.success(notification);
    }

    @Operation(summary = "分页查询通知列表", description = "分页查询通知列表")
    @GetMapping
    public Result<IPage<Notification>> getNotificationPage(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) String type,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String keyword) {
        Page<Notification> pageParam = new Page<>(page, pageSize);
        IPage<Notification> result = notificationService.getNotificationPage(pageParam, type, status, keyword);
        return Result.success(result);
    }

    @Operation(summary = "发送通知", description = "发送通知")
    @PostMapping("/{id}/send")
    public Result<Void> sendNotification(@PathVariable Long id) {
        notificationService.sendNotification(id);
        return Result.success();
    }

    @Operation(summary = "批量发送通知", description = "批量发送通知")
    @PostMapping("/batch-send")
    public Result<Void> batchSendNotifications(@RequestBody Map<String, List<Long>> params) {
        List<Long> ids = params.get("ids");
        notificationService.batchSendNotifications(ids);
        return Result.success();
    }

    @Operation(summary = "获取通知统计", description = "获取通知统计数据")
    @GetMapping("/stats")
    public Result<Map<String, Object>> getNotificationStats() {
        Map<String, Object> stats = notificationService.getNotificationStats();
        return Result.success(stats);
    }

    @Operation(summary = "获取我的未读通知", description = "获取当前用户的未读通知")
    @GetMapping("/unread")
    public Result<List<Notification>> getUnreadNotifications() {
        Long userId = StpUtil.getLoginIdAsLong();
        List<Notification> notifications = notificationService.getUserUnreadNotifications(userId);
        return Result.success(notifications);
    }

    @Operation(summary = "标记为已读", description = "标记通知为已读")
    @PostMapping("/{id}/read")
    public Result<Void> markAsRead(@PathVariable Long id) {
        Long userId = StpUtil.getLoginIdAsLong();
        notificationService.markAsRead(id, userId);
        return Result.success();
    }
}

